package com.zzc.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class RoutreController {

    @RequestMapping({"/","/index"})
    public String index(){
        return "index";
    }

    @RequestMapping("/tologin")
    public String tologin(){
        return "views/login";
    }

    @PostMapping("/login")
    public String login(String username, String password,boolean rememberMe, Model model){
        Subject subject = SecurityUtils.getSubject();
        System.out.println(rememberMe);
        UsernamePasswordToken token = new UsernamePasswordToken(username,password,rememberMe);
        try {
            subject.login( token );//if no exception, that's it, we're done!
            return "index";
        } catch ( UnknownAccountException uae ) {
            model.addAttribute("msg","用户名错误");//username wasn't in the system, show them an error message?
            return "views/login";
        } catch ( IncorrectCredentialsException ice ) {
            model.addAttribute("msg","密码错误");//password didn't match, try again?
            return "views/login";
        } catch ( LockedAccountException lae ) {
            model.addAttribute("msg","账号被锁定");//account for that username is locked - can't login.  Show them a message?
            return "views/login";
        }
//          catch ( AuthenticationException ae ) {
//            // ... more types exceptions to check if you want ...
//           //unexpected condition - error?
//        }
    }

    //注销
    @RequestMapping("/logout")
    public String logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "index";
    }

    @RequestMapping("/unauthorized")
    @ResponseBody
    public String unauthorized(){
        return "未授权";
    }

    @RequestMapping("/level1/{id}")
    public String level1(@PathVariable("id") String id){
        return "views/level1/"+id;
    }

    @RequestMapping("/level2/{id}")
    public String level2(@PathVariable("id") String id){
        return "views/level2/"+id;
    }

    @RequestMapping("/level3/{id}")
    public String level3(@PathVariable("id") String id){
        return "views/level3/"+id;
    }
}
